package org.pku_uestc.hackathon.sensor;

import java.util.Calendar;

import org.pku_uestc.hackathon.bean.Sensor;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;

/**
 * 根据api接口的控制类
 * 
 * @author Jason_wbw
 */

public class SmartLight extends Sensor {

	private static SparseArray<SmartLight> array = new SparseArray<SmartLight>(50);
	
	private int id;
	
	private SmartLight(int id) {
		super();
		this.id = id;
	}
	
	public static SmartLight getInstantces(int id){
		synchronized (array) {
			if(array.get(id)==null){
				SmartLight sl = new SmartLight(id);
				array.append(id, sl);
			}
			return array.get(id);
		}
	}

	public static abstract class SmartLightHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			String bundleResult = msg.getData().getString("RESPONSE"); // get
																		// the
																		// response
			if (bundleResult.trim()
					.equals("{\"success\":true,\"reason\":null}")) {
				successRefresh();
			} else {
				errorRefresh();
			}
		}

		protected abstract void successRefresh();

		protected abstract void errorRefresh();
	}

	/**
	 * 更改开关状态和相关参数
	 * 
	 * @param name
	 * @param turnOn
	 * @param brightness
	 * @param hue
	 * @return
	 */
	public boolean changeState(String name, boolean turnOn,
			String brightness, String hue, SmartLightHandler handler) {
		// TODO Auto-generated method stub
		if (token == null)
			return false;
		Log.d(getClass().getName(), "changeState");

		String data = "{\"data\":" + "[{\"id\":" + id
				+ (name == null ? "" : ",\"name\":" + name) + ",\"turned_on\":"
				+ turnOn
				+ (brightness == null ? "" : ",\"brightness\":" + brightness)
				+ (hue == null ? "" : ",\"hue\":" + hue) + "}]" + ","
				+ "\"auth_token\":\"" + token + "\"}";
		performPutRequest(handler,
				"https://huantengsmart.com/bulbs/request_update.json", data);
		return true;
	}

	/**
	 * 不更改开关状态
	 * 
	 * @param id
	 * @param name
	 * @param brightness
	 * @param hue
	 * @return
	 */
	public boolean changeInfo(String name, String brightness,
			String hue, SmartLightHandler handler) {
		// TODO Auto-generated method stub
		if (token == null)
			return false;
		if (name == null && brightness == null && hue == null) {
			return false;
		}

		String data = "{\"data\":" + "[{\"id\":" + id
				+ (name == null ? "" : ",\"name\":" + name) + ",\"turned_on\":"
				+ (brightness == null ? "" : ",\"brightness\":" + brightness)
				+ (hue == null ? "" : ",\"hue\":" + hue) + "}]" + ","
				+ "\"auth_token\":\"" + token + "\"}";
		this.performPutRequest(handler,
				"https://huantengsmart.com/bulbs/request_update.json", data);
		return true;
	}
	
	public void action(){
		SmartLightHandler handler = new SmartLightHandler() {
			private int times = 0;
			private long lastTime = 0;
			@Override
			protected void successRefresh() {
				// TODO Auto-generated method stub
				while((Calendar.getInstance().getTimeInMillis()-lastTime)/1000<1){}
				times++;
				lastTime = Calendar.getInstance().getTimeInMillis();
				
				if(times>10)
					return;
				if(times%2==0)
					changeState(null, true, 0.1+"", 0.1+"",
							this);
				else
					changeState(null, true, 0.9+"", 0.9+"",
							this);
			}

			@Override
			protected void errorRefresh() {
				// TODO Auto-generated method stub
			}
		};
		
		changeState(null, true, 0.4+"", 0.4+"",
				handler);
	}

	@Override
	public int getType() {
		// TODO Auto-generated method stub
		return TYPE_SMARTLIGHT;
	}

	@Override
	public void request(int timer) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public int getId() {
		// TODO Auto-generated method stub
		return id;
	}
}
